<?php

namespace App\Services;

use App\Models\Recruit;

class PublishService extends BaseService
{
    public function getPublishRecruitListByUser(array $input, int $page, int $limit, string $sortField,
                                            string $orderRule, array $recruitTableColumns = ['*'], array $where = [])
    {
        $query = Recruit::query()->where('user_id', '=', $input['user_id']);

        if ($input['status'] == 0) {
            $query->where('status', '>', 0);
        } else {
            $query->where($where);
        }

        if (!empty($sortField) && !empty($orderRule)) {
            $query = $query->orderBy($sortField, $orderRule);
        }

        $paginator = $query->paginate($limit, $recruitTableColumns, 'page', $page);
        return $this->optimizationPaginate($paginator);
    }

    public function editRecruitStatus($input)
    {
        $actionType = $input['action_type'];
        $status = null;
        if ($actionType == 'delete') {
            $status = -1;
        } else if ($actionType == 'stop') {
            $status = 3;
        }
        // todo: 这里要验证 id 的发布人是否和当前操作的用户是一样的，这里还要记录日志
        Recruit::query()->where('id', $input['id'])->update(['status' => $status]);
    }
}
